<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>
              江苏传智播客教育科技股份有限公司
            </span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">
                      添加子部门
                    </el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 树形组件 -->
        <el-tree :data="list">
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <svg-icon icon-class="home" />
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">
                          添加子部门
                        </el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">
                          编辑子部门
                        </el-dropdown-item>
                        <el-dropdown-item
                          v-if="data.children && data.children.length === 0"
                          @click.native="hDel(data.id)"
                        >
                          删除子部门
                        </el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <el-dialog
        :title="this.isEdit ? '编辑子部门' : '新增子部门'"
        :visible.sync="showDialog"
        :close-on-press-escape="false"
        :close-on-click-modal="false"
        @close="hClose"
      >
        <!-- 解决数据不更新的问题1 -->
        <!-- v-if="showDialog" -->
        <deptDialog
          :id="curId"
          ref="mdept"
          :is-edit="isEdit"
          :orange-list="orangeList"
          @success="hSuccess"
          @cancel="hCancel"
        />
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getDepartments, deleteDepartment } from '@/api/departments'
import { treeDataList } from '@/utils'
import deptDialog from './modules/deptDialog.vue'
export default {
  components: {
    deptDialog
  },
  data() {
    return {
      // 依赖一份树形数据
      list: [],
      // 控制弹出层的显示隐藏
      showDialog: false,
      curId: '',
      isEdit: true,
      orangeList: []
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    hClose() {
      this.$refs.mdept.hClear()
    },
    // 删除部门
    hDel(id) {
      try {
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(async() => {
            const res = await deleteDepartment(id)
            console.log(res)
            this.$message.success(res.message)
            this.loadDepartments()
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除'
            })
          })
      } catch (error) {
        console.log(error)
      }
    },
    // 点击取消关闭弹层
    hCancel() {
      this.showDialog = false
    },
    hSuccess() {
      // 关闭弹层
      this.showDialog = false
      // 重新获取数据
      this.loadDepartments()
    },
    // 获取到对应的id将赋给变量
    hAdd(id) {
      this.curId = id
      this.isEdit = false
      this.showDialog = true
    },
    async hEdit(id) {
      this.curId = id
      this.isEdit = true
      this.showDialog = true
      // 解决数据不更新的问题 方法 2
      await this.$nextTick(() => {
        this.$refs.mdept.loadDepartmentById()
      })
    },
    // 获取数据
    async loadDepartments() {
      try {
        const res = await getDepartments()

        // 删除数组元素的第一项
        res.data.depts.shift()
        console.log(res)
        this.list = treeDataList(res.data.depts)
        this.orangeList = res.data.depts.map((item) => {
          const { name, code, id, pid } = item
          return { name, code, id, pid }
        })
        console.log(this.orangeList, '123123')
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>
